package com.ccx.coachsite.ui.share;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.CustomInfo;
import com.ccx.coachsite.ui.own.OwerAdapterBase;

public class MyCustomListAdapter extends OwerAdapterBase<CustomInfo>{
	
	private Map<Integer, Boolean> selectedMap;
	
	public MyCustomListAdapter(Context context, List<CustomInfo> list) {
		super(context, list);
		
		selectedMap = new HashMap<Integer, Boolean>();  
        for (int i = 0; i < list.size(); i++) {  
            selectedMap.put(i, false);  
        } 
	}
	
	public void resetList(List<CustomInfo> list, boolean isSelected) {
		mList = null;
		mList = list;
		selectedMap.clear();
		for (int i = 0; i < list.size(); i++) {  
            selectedMap.put(i, isSelected);  
        }  
		super.notifyDataSetChanged();
	}
	
	public List<String> getInputs()
	{
		List<String> lstResult = new ArrayList<String>();
		for (int i = 0; i < selectedMap.size(); i++) {  
           if(selectedMap.get(i))
           {
        	   lstResult.add(mList.get(i).getTianYi());
           }
        }  
		
		return lstResult;
	}
	
	public Map<Integer, Boolean> getSelectedMap() {
		return selectedMap;
	}

	public void setSelectedMap(Map<Integer, Boolean> selectedMap) {
		this.selectedMap = selectedMap;
	}
	
	public void doSelectAll(boolean checked)
	{
		for (int i = 0; i < selectedMap.size(); i++) {  
            selectedMap.put(i, checked);  
        }  
		super.notifyDataSetChanged();
	}
	
	public void doSelectAtIndex(int index)
	{
		boolean b = !selectedMap.get(index);
		selectedMap.put(index, b);  
		super.notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView == null) {
			convertView = mInflater.inflate(R.layout.share_custom_list_item, parent, false);
			holder = new ViewHolder();
			holder.nUseGprs = (TextView) convertView.findViewById(R.id.share_custom_used_gprs);
			holder.nPhone = (TextView) convertView.findViewById(R.id.share_custom_phone);
			holder.nCheckbox = (ImageView) convertView.findViewById(R.id.share_custom_checkbox_image_item);
			convertView.setTag(holder);
		}else 
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.nUseGprs.setText(mList.get(position).getPackage_gprs());
		holder.nPhone.setText(mList.get(position).getTianYi());
		//holder.nCheckbox.setOnCheckedChangeListener(selectedCheckListener);
		if(!selectedMap.get(position))
			holder.nCheckbox.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_unchecked));
		else
			holder.nCheckbox.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_checked));

		return convertView;
	}
	
	OnCheckedChangeListener selectedCheckListener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			Integer pos = (Integer)buttonView.getTag();
			selectedMap.put(pos, isChecked);  
		}
	};
	
	class ViewHolder {
		TextView nUseGprs;
		TextView nPhone;
		ImageView nCheckbox;
	}
}
